<?php
require_once('Config/Http.php');
require_once('Config/Config.php');
require_once('Config/Tools.php');

require_once('Logs/Logs.php');
	

// config object
$config = new config();
$parameters = config::_instal_ini();

// zaloż baze dla subdomeny
if( /*$config->__isset("subdomena") &&*/ is_file($parameters['sqlite']['host'].str_replace('www.','',$_SERVER['HTTP_HOST']).".db")){
	$parameters['sqlite']['database'] =  str_replace('www.','',$_SERVER['HTTP_HOST']).".db";
	$parameters['drivers']['urlspanel'] = $parameters['drivers']['subpanel'] ;
}else{
	//if($config->__isset("subdomena")){
		$arr = unserialize(MAIN_URL);
		header("Location: http://".$arr[0]);
	//}
}


$smarty = config::_get_smarty_obj();
$dbl = config::getDB($parameters);
config::_instal_database($parameters['drivers']['driver']);
$config->cmsSettings = tools::__adodb('getAssoc',"SELECT id, wartosc FROM ".SETUP);


require_once("Session/class_session_{$parameters['drivers']['driver']}.php");
// session object
$objSession = new user_session();
$objSession->Impress();	
// install language
$config->_get_language();

try {	
	$dispatcher = new Loader_Dispatcher();	
	// wywołanie clasy z urla
	if ($config->__isset('parameters')){
		$dispatcher->handleEvent($config->parameters);
	} 	
	
	// wygląd
	$settings = $config->themeSettings();
	tools::_assign("settings",$settings);
	tools::_assign('info',$parameters['info']);
	tools::_assign('cms',$parameters['cms']);
	
	// generuj plik oferta.txt
	if ( $config->__logg && isset($_REQUEST['parameters']) &&  preg_match("/,oferta,/", $_REQUEST['parameters']) )  ob_start("zapiszOferte");
	
	$dispatcher->handleEvent("Panel_Theme");
	
	// generuj plik oferta.txt
	if ( $config->__logg && isset($_REQUEST['parameters']) &&  preg_match("/,oferta,/", $_REQUEST['parameters']) ) ob_end_flush();
			
} catch ( Exception $e ){
	die("blad strony...".$e->getMessage());
}

if ($parameters['sqlite']['debugging'] ) echo "<div style=\"text-align: left;	\">".$config->debuging_query_data."</div>";
if ($parameters['testy']['speed'] ) echo "<!--{$config->runtime}-->";
/**
 * buforuje stronę
 * zapisuje jej kod do pliku i wyswietla na ekran
 * 
 * @param $buffer
 * @return unknown_type
 */
function zapiszOferte($buffer){
	global $dispatcher;
	$log = new Logs_Logs($dispatcher->object['AllegroController']->photoAllegroPath."oferta.html");
	// usun niepotrzebne dane
	$remove = '/<!--panel-->(.*?)<!--\/panel-->/s';
	$buffer__ = preg_replace($remove, '', $buffer);
	$log->add($buffer__);
	return $buffer;
}
